{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3a19dfcd-f3e0-4b1b-9cde-d721571e3c14",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-04-18T15:39:24.380967Z",
     "iopub.status.busy": "2024-04-18T15:39:24.380605Z",
     "iopub.status.idle": "2024-04-18T15:39:24.861590Z",
     "shell.execute_reply": "2024-04-18T15:39:24.861271Z",
     "shell.execute_reply.started": "2024-04-18T15:39:24.380941Z"
    }
   },
   "outputs": [],
   "source": [
    "from web3 import Web3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a0e00864-e412-4ba1-8db0-2305fae5b099",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-04-18T15:43:31.589302Z",
     "iopub.status.busy": "2024-04-18T15:43:31.588718Z",
     "iopub.status.idle": "2024-04-18T15:43:31.797756Z",
     "shell.execute_reply": "2024-04-18T15:43:31.796115Z",
     "shell.execute_reply.started": "2024-04-18T15:43:31.589275Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w3 = Web3(Web3.HTTPProvider(\"https://sepolia.infura.io/v3/824256f39eb744afb721dddb03a36600\"))\n",
    "w3.is_connected()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c9b81a18-d063-45b0-bae8-552a152f2f5f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-04-18T15:43:20.685208Z",
     "iopub.status.busy": "2024-04-18T15:43:20.684834Z",
     "iopub.status.idle": "2024-04-18T15:43:20.688500Z",
     "shell.execute_reply": "2024-04-18T15:43:20.687781Z",
     "shell.execute_reply.started": "2024-04-18T15:43:20.685185Z"
    }
   },
   "source": [
    "## Block info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "66346816-4dcf-4fdd-989f-8f549f0e6c5f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-04-18T15:45:43.450514Z",
     "iopub.status.busy": "2024-04-18T15:45:43.450153Z",
     "iopub.status.idle": "2024-04-18T15:45:43.676788Z",
     "shell.execute_reply": "2024-04-18T15:45:43.676043Z",
     "shell.execute_reply.started": "2024-04-18T15:45:43.450488Z"
    }
   },
   "outputs": [],
   "source": [
    "# We will operate with 5725600 block\n",
    "block = w3.eth.get_block(5725600)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "28fce338-429f-40eb-92ae-7160d47ad80e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-04-18T15:55:28.076539Z",
     "iopub.status.busy": "2024-04-18T15:55:28.076061Z",
     "iopub.status.idle": "2024-04-18T15:55:28.082747Z",
     "shell.execute_reply": "2024-04-18T15:55:28.082083Z",
     "shell.execute_reply.started": "2024-04-18T15:55:28.076514Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "HexBytes('0x8e315bffa68b2015314e2f9af5b6e813661a912e8710c01f27a1c1d91ca19801')"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# We will get sixth trasaction from this block\n",
    "# Cause this transaction is simple ETH transfer\n",
    "tx = block['transactions'][6]\n",
    "tx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "531911dd-eab8-4c2c-87b1-5d109261c445",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-04-18T15:55:28.588932Z",
     "iopub.status.busy": "2024-04-18T15:55:28.588574Z",
     "iopub.status.idle": "2024-04-18T15:55:28.783473Z",
     "shell.execute_reply": "2024-04-18T15:55:28.782802Z",
     "shell.execute_reply.started": "2024-04-18T15:55:28.588907Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'https://sepolia.etherscan.io/tx/0x8e315bffa68b2015314e2f9af5b6e813661a912e8710c01f27a1c1d91ca19801'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "AttributeDict({'blockHash': HexBytes('0x480d02f3a693c61a93732523dfe3fdaf418bee4696d406dbd7441832517aac41'),\n",
       " 'blockNumber': 5725600,\n",
       " 'chainId': 11155111,\n",
       " 'from': '0x625BCC1142E97796173104A6e817Ee46C593b3C5',\n",
       " 'gas': 21000,\n",
       " 'gasPrice': 3960814074,\n",
       " 'hash': HexBytes('0x8e315bffa68b2015314e2f9af5b6e813661a912e8710c01f27a1c1d91ca19801'),\n",
       " 'input': HexBytes('0x'),\n",
       " 'nonce': 57092,\n",
       " 'r': HexBytes('0xe37982309c033274f1ca44c1f06c522f7089c1e01e036d608294d6288ef88a13'),\n",
       " 's': HexBytes('0x549855f763056b2d0f839cf7d3d868443083f0570033a96f25212ed1f4881238'),\n",
       " 'to': '0xde692e4c250B6840bB512215db81ff5c606866C9',\n",
       " 'transactionIndex': 6,\n",
       " 'type': 0,\n",
       " 'v': 22310258,\n",
       " 'value': 100000000000000000})"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Lets get info about transaction\n",
    "tx_info = w3.eth.get_transaction(tx)\n",
    "hash = tx_info['hash']\n",
    "gas = tx_info['gas']\n",
    "gasPrice = tx_info['gasPrice']\n",
    "maxFeePerGas = tx_info.get('maxFeePerGas', None)\n",
    "maxPriorityFeePerGas = tx_info.get('maxPriorityFeePerGas', None)\n",
    "hash = tx_info['hash']\n",
    "display(f\"https://sepolia.etherscan.io/tx/{hash.hex()}\")\n",
    "tx_info"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "623128a8-360c-4ec7-9ad2-6a28f4f0cc18",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-04-18T15:48:14.346650Z",
     "iopub.status.busy": "2024-04-18T15:48:14.346278Z",
     "iopub.status.idle": "2024-04-18T15:48:14.351707Z",
     "shell.execute_reply": "2024-04-18T15:48:14.350985Z",
     "shell.execute_reply.started": "2024-04-18T15:48:14.346626Z"
    }
   },
   "source": [
    "* Note here *\n",
    "> Txs without intup or input = '0x' are plaint transaction / non Smartcontract calls\n",
    "> Thus, no variants with gas here, there aren't any EVM execution and you get constant comission"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "5fcf62e7-172a-4db5-aa4c-4deefc466871",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-04-18T15:57:41.397903Z",
     "iopub.status.busy": "2024-04-18T15:57:41.397492Z",
     "iopub.status.idle": "2024-04-18T15:57:41.402431Z",
     "shell.execute_reply": "2024-04-18T15:57:41.401741Z",
     "shell.execute_reply.started": "2024-04-18T15:57:41.397875Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "83177095554000"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gas * gasPrice"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "78ad6db0-db1f-4f1f-acfb-da5ffa166041",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-04-18T15:58:49.812655Z",
     "iopub.status.busy": "2024-04-18T15:58:49.812323Z",
     "iopub.status.idle": "2024-04-18T15:58:49.818631Z",
     "shell.execute_reply": "2024-04-18T15:58:49.817255Z",
     "shell.execute_reply.started": "2024-04-18T15:58:49.812632Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0.000083177095554'"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "commission = w3.from_wei(gas * gasPrice, unit='ether')\n",
    "f\"{commission:.18}\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "fad30021-3c9f-49bf-b3cd-4ac135b979d6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-04-18T15:59:52.779670Z",
     "iopub.status.busy": "2024-04-18T15:59:52.779318Z",
     "iopub.status.idle": "2024-04-18T15:59:52.783139Z",
     "shell.execute_reply": "2024-04-18T15:59:52.782379Z",
     "shell.execute_reply.started": "2024-04-18T15:59:52.779644Z"
    }
   },
   "outputs": [],
   "source": [
    "# chech that equal to Etherscan info\n",
    "# no AsssertionError means equals\n",
    "assert '0.000083177095554' == str(commission)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
